var express = require('express');
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();
var path = require('path');
/* GET users listing. */
router.get('/', function (req, res, next) {
    // if(req.query.test_login== "1"){
    // if(req.session.username == undefined || req.session.username == "")
    // res.send('0');
    // }
    // else{
    var db = new sqlite3.Database(path.join(__dirname, "../db/LH.s3db"));
    try {
        sqlStr1 = "UPDATE activity SET status=1 where a_name=?";
        sqlStr2 = "UPDATE activity SET status=-1 where a_name=?";
        sqlStr3 = "UPDATE activity SET status=2 where a_name=?";
        sqlStr4 = "INSERT INTO a_suggestion VALUES(?,?,?,?,?)";
        var aname = req.query.aname;
        if (req.query.status == 1) {
            db.all(sqlStr1, [aname], function (err) {
                if (err == null) {
                    res.send("1");
                } else {
                    res.send("0");
                }
            });
        } else if (req.query.status == -1) {
            db.all(sqlStr2, [aname], function (err) {
                if (err == null) {
                    res.send("1");
                } else {
                    res.send("0");
                }
            });
        } else if (req.query.status == 2) {
            var com_id = req.session.username;
            sqlStr5 = "SELECT * FROM activity where a_name=?";
            sqlStr6 = "select * from a_suggestion order by sug_id desc limit 0,1";
            db.all(sqlStr5, [req.query.aname], function (err, rows) {
                if (rows) {
                    var a_no = rows[0]["a_no"];
                    db.all(sqlStr6, function (err, rows) {
                        if (rows) {
                            var sug_id = parseInt(rows[0]["sug_id"]) + 1;
                            var content = req.query.content;
                            var nowDate = new Date();
                            var year = nowDate.getFullYear();
                            var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate
                                .getMonth() + 1) : nowDate.getMonth() + 1;
                            var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() :
                                nowDate.getDate();
                            var dateStr = year + "-" + month + "-" + day;
                                db.all(sqlStr4, [sug_id, a_no, com_id, content,dateStr], function (err) {
                                    db.all(sqlStr3, [aname], function (err) {
                                        if (err == null) {
                                            res.send("1");
                                        } else {
                                            res.send("0");
                                        }
                                    });
                                });
                           
                        }
                    })
                }
            })

        }
    } catch (ex) {
        console.log(ex);
    };
});
module.exports = router;